﻿using AnQinManagermentBackendAPI.Common;
using AnQinManagermentBackendAPI.Common.DateBase;
using AnQinManagermentBackendAPI.Common.Request;
using AnQinManagermentBackendAPI.Model.DTO.CodeRule;
using AnQinManagermentBackendAPI.Model.DTO.ImplementManagement;
using AnQinManagermentBackendAPI.Model.Entity;
using AnQinManagermentBackendAPI.Model.View.ImplementManagement;
using Microsoft.EntityFrameworkCore;

namespace AnQinManagermentBackendAPI.Service
{
    public class ImplementManagementService
    {
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 构造注入
         */
        public ImplementManagementService(DataContext context)
        {
            _context = context;
        }

        /**
         * 获取实施管理列表
         */
        public async Task<RequestResponseModel<PageResult<ImplementManagementView>>> GetImplementManagementList(ImplementManagementSearchDTO implementManagementSearchDTO)
        {
            //筛选
            var query = _context.ImplementManagementView
                .WhereIF(a => a.Code.Contains(implementManagementSearchDTO.Code), !string.IsNullOrEmpty(implementManagementSearchDTO.Code))
                .WhereIF(a => a.Name.Contains(implementManagementSearchDTO.Name), !string.IsNullOrEmpty(implementManagementSearchDTO.Name))
                .WhereIF(a => a.StaffId == implementManagementSearchDTO.StaffId, !string.IsNullOrEmpty(implementManagementSearchDTO.StaffId))
                .WhereIF(a => a.ProjectId == implementManagementSearchDTO.ProjectId, !string.IsNullOrEmpty(implementManagementSearchDTO.ProjectId))
                .WhereIF(a => a.DevelopId == implementManagementSearchDTO.DevelopId,!string.IsNullOrEmpty(implementManagementSearchDTO.DevelopId))
                ;

            //返回列表
            var implementManagement = await query
                .OrderByDescending(a => a.CreateTime)
                .Page(implementManagementSearchDTO.CurrentPage, implementManagementSearchDTO.PageSize)
                .ToListAsync();

            //总数据量
            var pageCount = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<ImplementManagementView>(implementManagement, pageCount);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }

        /**
         * 新增实施管理
         */
        public async Task<RequestResponseModel<string>> AddImplementManagement(ImplementManagementAddDTO implementManagementAddDTO)
        {
            //操作数据
            DateTime date = DateTime.Now;
            Guid guid = Guid.NewGuid();

            //生成新数据
            var implementManagement = new ImplementManagementModel
            {
                Id = guid.ToString(),
                Code = implementManagementAddDTO.Code,
                Name = implementManagementAddDTO.Name,
                StaffId = implementManagementAddDTO.StaffId,
                ProjectId = implementManagementAddDTO.ProjectId,
                DevelopId = implementManagementAddDTO.DevelopId,
                State = StateEnum.ImplementStateEnum.待实施,
                File = implementManagementAddDTO.File,
                Remark = implementManagementAddDTO.Remark,
                StartTime = implementManagementAddDTO.StartTime,
                EndTime = implementManagementAddDTO.EndTime,
                CreateTime = date,
            };

            //新增开发管理
            _context.ImplementManagement.Add(implementManagement);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "新增成功", guid.ToString());
        }

        /**
         * 更新实施管理
         */
        public async Task<RequestResponseModel<string>> PutImplementManagement(ImplementManagementUpdateDTO implementManagementUpdateDTO)
        {
            //查找数据
            var query = await _context.ImplementManagement.FindAsync(implementManagementUpdateDTO.Id);

            //校验数据
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", implementManagementUpdateDTO.Id);
            };

            //生成新数据
            query.Name = implementManagementUpdateDTO.Name;
            query.StaffId = implementManagementUpdateDTO.StaffId;
            query.ProjectId = implementManagementUpdateDTO.ProjectId;
            query.DevelopId = implementManagementUpdateDTO.DevelopId;
            query.File = implementManagementUpdateDTO.File;
            query.Remark = implementManagementUpdateDTO.Remark;
            query.StartTime = implementManagementUpdateDTO.StartTime;
            query.EndTime = implementManagementUpdateDTO.Endtime;

            //更新开发管理表
            _context.ImplementManagement.Update(query);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "更新成功", implementManagementUpdateDTO.Id);
        }

        /**
         * 删除实施管理
         */
        public async Task<RequestResponseModel<string>> DeleteImplementManagement(string Id)
        {
            //判空
            if (string.IsNullOrEmpty(Id))
            {
                return RequestResponse.CreateResponse(400, "Id为空", Id);
            };

            //校验Id有效性
            var query = await _context.ImplementManagement.FindAsync(Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            };

            //删除操作
            _context.ImplementManagement.Remove(query);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "删除成功", Id);
        }

        /**
         * 完成实施
         */
        public async Task<RequestResponseModel<string>> ConfirmImplementManagement(string Id)
        {
            //判空
            if (string.IsNullOrEmpty(Id))
            {
                return RequestResponse.CreateResponse(400, "Id为空", Id);
            };

            //校验Id有效性
            var query = await _context.ImplementManagement.FindAsync(Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            };

            //查找项目
            var project = await _context.Project.FindAsync(query.ProjectId);

            if (project == null)
            {
                return RequestResponse.CreateResponse(400, "找不到项目", Id);
            }

            //查找销售表
            var saleManagement = await _context.SaleManagement.SingleOrDefaultAsync(a => a.ProjectId == query.ProjectId);

            if (saleManagement == null)
            {
                return RequestResponse.CreateResponse(400, "找不到销售单据", Id);
            }

            //更改数据
            query.State = StateEnum.ImplementStateEnum.已完成;
            project.State = StateEnum.ProjectStateEnum.已完成;
            saleManagement.State = StateEnum.SaleStateEnum.已完成;

            //保存库
            _context.ImplementManagement.Update(query);
            _context.Project.Update(project);
            _context.SaleManagement.Update(saleManagement);
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "实施成功", Id);
        }

        /**
         * 仍有问题
         */
        public async Task<RequestResponseModel<string>> ProblemImplementManagement(string Id)
        {
            //判空
            if (string.IsNullOrEmpty(Id))
            {
                return RequestResponse.CreateResponse(400, "Id为空", Id);
            };

            //校验Id有效性
            var query = await _context.ImplementManagement.FindAsync(Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            };

            //查找项目
            var project = await _context.Project.FindAsync(query.ProjectId);

            if (project == null)
            {
                return RequestResponse.CreateResponse(400, "找不到项目", Id);
            }

            //更改数据
            query.State = StateEnum.ImplementStateEnum.仍有问题;
            project.State = StateEnum.ProjectStateEnum.开发中;

            //保存库
            _context.ImplementManagement.Update(query);
            _context.Project.Update(project);
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "实施成功", Id);
        }
    }
}
